package com.example.blogspring.service;

import com.example.blogspring.mapper.BlogMapper;
import com.example.blogspring.mapper.UserMapper;
import com.example.blogspring.model.BlogInfo;
import com.example.blogspring.model.UserInfo;
import com.example.blogspring.utils.SecurityUtils;
import jdk.nashorn.internal.ir.CallNode;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/22 3:27
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper ;
    @Autowired
    BlogMapper blogMapper;
    public Integer add(UserInfo userInfo){
        String password = userInfo.getPassword();
        String finalPassword = SecurityUtils.encrypt(password);
        userInfo.setPassword(finalPassword);
        return userMapper.add(userInfo);
    }

    public UserInfo getUserByName(String username){
        return userMapper.getUserByName(username);
    }

    public UserInfo getUserInfoByBlogId(Integer blogId){
        BlogInfo blogById = blogMapper.getBlogById(blogId);
        if (blogById == null || blogById.getUserId()<0){
            return null;
        }
        return userMapper.getUserById(blogById.getUserId());
    }
}
